package com.fancon.android.gardenjokesx.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;

import com.fancon.android.gardenjokesx.R;

public class TimeUtil {
	/**
	 * Count down time from post
	 * @param second long
	 * @param cont Context
	 * @return String 
	 * ex: 5 seconds ago...
	 */
	public static String getTimeCount(long second, Context cont) {
		String re = "";
		try {
			Resources r = cont.getResources();
			if (second < 0) {
				return re;
			} else if (second < 60) {			
				if (second <= 1) {
					re = r.getString(R.string.keyTimeSecond, String.valueOf(second));
				} else {
					re = r.getString(R.string.keyTimeSeconds, String.valueOf(second));
				}
			} else if (second < 3600) {			
				if (second / 60 == 1) {
					re = r.getString(R.string.keyTimeMinute, String.valueOf(second / 60));
				} else {
					re = r.getString(R.string.keyTimeMinutes, String.valueOf(second / 60));
				}
			} else if (second < 86400) {
				if (second / 3600 == 1) {
					re = r.getString(R.string.keyTimeHour, String.valueOf(second / 3600));
				} else {
					re = r.getString(R.string.keyTimeHours, String.valueOf(second / 3600));
				}
			} else if (second < 604800) {
				if (second / 86400 == 1) {
					re = r.getString(R.string.keyTimeDay, String.valueOf(second / 86400));
				} else {
					re = r.getString(R.string.keyTimeDays, String.valueOf(second / 86400));
				}
			} else {
				if (second / 604800 == 1) {
					re = r.getString(R.string.keyTimeWeek, String.valueOf(second / 604800));
				} else {
					re = r.getString(R.string.keyTimeWeeks, String.valueOf(second / 604800));
				}
			}
		} catch (Exception e) {
			// do nothing
		}
		return re;
	}
	/**
	 * Dùng để set thời gian lần cuối refresh, 
	 * @param context: Context get SharedPrefs 
	 * @param position: ScreenID of Meshtiles (Mã màn hình trong file Constant)
	 */
	public static void setLastRefresh(Context context,int position) {
		SharedPreferences settings = context.getSharedPreferences(
				Constant.PREFS_NAME, 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putString("ScreenID"+position, refreshTime());
		editor.commit();
	}
	
	/**
	 * 
	 * @param context: context get Prefs
	 * @param position: Screen use it
	 * @return: Last refreshed
	 */
	public static String getLastRefresh(Context context,int position) {
		SharedPreferences settings = context.getSharedPreferences(
				Constant.PREFS_NAME, 0);
		String label= context.getString(R.string.label_last_refresh);
		String time = settings.getString("ScreenID"+position, "Null");
		return label+" "+time;
	}
	
	/**
	 * Create string time when refresh data
	 * @return String time
	 */
	public static final String refreshTime() {
		 //create Date object
	     Date date = new Date();
	   
	     //formatting time to have AM/PM text using 'a' format
	     String strDateFormat = "MM/dd/yy HH:mm a";
	     SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
	     
	     return sdf.format(date);
	}
	
	public static final String getCurrentTime(){
		SimpleDateFormat s = new SimpleDateFormat("ddMMyyyyhhmmss");
		String format = s.format(new Date());
		return format;
	}
}
